package cn.gaily.eduservice.controller;

import cn.gaily.commonutils.R;
import cn.gaily.eduservice.entity.EduTeacher;
import cn.gaily.eduservice.entity.vo.TeacherQuery;
import cn.gaily.eduservice.service.EduTeacherService;
import cn.gaily.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-05-22
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")

public class EduTeacherController {
    @Autowired
    EduTeacherService eduTeacherService;

    //查询讲师
    @ApiOperation("查询讲师")
    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items", list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "逻辑删除讲师", required = true) @PathVariable String id) {
        Boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //分页查询讲师
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{size}")
    public R pageTeacher(@PathVariable Long current, @PathVariable Long size) {
        Page<EduTeacher> pageTeacher = new Page<>(current, size);//当前页数和总页数
        //把数据都封装到pageTeacher中
        eduTeacherService.page(pageTeacher, null);
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        //total
        long total = pageTeacher.getTotal();
        return R.ok().data("total", total).data("item", records);
    }

    @ApiOperation(value = "条件查询分页")
    @PostMapping("pageTeacherCondition/{current}/{size}")
    public R teacherQuery(@PathVariable Long current,
                          @PathVariable Long size,
                          @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, size);
        //构建条件wrapper
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //wrapper
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        String name = teacherQuery.getName();
        //如果不为空字符串则拼接条件
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);//大于等于
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);//小于等于
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(pageTeacher, wrapper);
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        //total
        long total = pageTeacher.getTotal();

        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        } else return R.error();
    }

    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher", byId);
    }

    @ApiOperation(value = "讲师修改")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean updateById = eduTeacherService.updateById(eduTeacher);

        if(updateById) {
            return R.ok();
        } else return R.error();
    }

}

